<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">






  
  
    
      
    
    
      
    
  <script src="https://cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
  <link href="https://cdn.bootcss.com/pace/1.0.2/themes/blue/pace-theme-flash.min.css" rel="stylesheet">







<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















  

<link href="https://cdn.bootcss.com/font-awesome/4.6.2/css/font-awesome.min.css" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.3.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.3.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.3.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.3.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.3.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '6.3.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="vue与react组件对比学习前言我最一开始是先学的react，然后也就前段时间开始学习vue，一开始给我的感受是两者很相似，react给我的感觉是灵活，vue是一种死板的感觉。为什么有这种感觉呢，react有一种很强烈的欲望，all in js只要能够用够js写的，就全用js写，所以一切都很灵活，jsx很酷，高阶组件牛逼，es7的装饰器也是可以玩的飞起。反观vue，搞的.vue单文件还是尽量保留">
<meta name="keywords" content="react,vue">
<meta property="og:type" content="article">
<meta property="og:title" content="vue与react组件对比学习">
<meta property="og:url" content="http://yoursite.com/2018/08/03/vue与react组件对比学习/index.html">
<meta property="og:site_name" content="三月的米店">
<meta property="og:description" content="vue与react组件对比学习前言我最一开始是先学的react，然后也就前段时间开始学习vue，一开始给我的感受是两者很相似，react给我的感觉是灵活，vue是一种死板的感觉。为什么有这种感觉呢，react有一种很强烈的欲望，all in js只要能够用够js写的，就全用js写，所以一切都很灵活，jsx很酷，高阶组件牛逼，es7的装饰器也是可以玩的飞起。反观vue，搞的.vue单文件还是尽量保留">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://yoursite.com/2018/08/03/vue与react组件对比学习/877090454.jpg">
<meta property="og:updated_time" content="2018-12-03T08:10:43.245Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="vue与react组件对比学习">
<meta name="twitter:description" content="vue与react组件对比学习前言我最一开始是先学的react，然后也就前段时间开始学习vue，一开始给我的感受是两者很相似，react给我的感觉是灵活，vue是一种死板的感觉。为什么有这种感觉呢，react有一种很强烈的欲望，all in js只要能够用够js写的，就全用js写，所以一切都很灵活，jsx很酷，高阶组件牛逼，es7的装饰器也是可以玩的飞起。反观vue，搞的.vue单文件还是尽量保留">
<meta name="twitter:image" content="http://yoursite.com/2018/08/03/vue与react组件对比学习/877090454.jpg">






  <link rel="canonical" href="http://yoursite.com/2018/08/03/vue与react组件对比学习/"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>vue与react组件对比学习 | 三月的米店</title>
  









  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">三月的米店</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <h1 class="site-subtitle" itemprop="description">今儿不吃饭</h1>
      
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">
    <a href="/tags/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />标签</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">
    <a href="/categories/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-th"></i> <br />分类</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档</a>
  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/08/03/vue与react组件对比学习/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Mark Lee">
      <meta itemprop="description" content="折腾小久的博客,在 2018-08-01 终于基本成型啦，以后就常驻啦">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="三月的米店">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">vue与react组件对比学习
              
            
          </h2>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2018-08-03 09:29:03" itemprop="dateCreated datePublished" datetime="2018-08-03T09:29:03+08:00">2018-08-03</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">更新于</span>
                
                <time title="修改时间：2018-12-03 16:10:43" itemprop="dateModified" datetime="2018-12-03T16:10:43+08:00">2018-12-03</time>
              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/前端/" itemprop="url" rel="index"><span itemprop="name">前端</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="vue与react组件对比学习"><a href="#vue与react组件对比学习" class="headerlink" title="vue与react组件对比学习"></a>vue与react组件对比学习</h1><h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>我最一开始是先学的<code>react</code>，然后也就前段时间开始学习<code>vue</code>，一开始给我的感受是两者很相似，<code>react</code>给我的感觉是<em>灵活</em>，<code>vue</code>是一种<em>死板</em>的感觉。为什么有这种感觉呢，<code>react</code>有一种很强烈的欲望，<strong>all in js</strong>只要能够用够<code>js</code>写的，就全用<code>js</code>写，所以一切都很灵活，<code>jsx</code>很酷，<strong>高阶组件</strong>牛逼，<code>es7</code>的装饰器也是可以玩的飞起。反观<code>vue</code>，搞的<code>.vue单文件</code>还是尽量保留原来前端开发的模式，留下了<code>template</code>、<code>script</code>、<code>style</code>三个最基本的东西，其实内部运作应该还是和<code>react</code>类似。</p>
<h2 id="react"><a href="#react" class="headerlink" title="react"></a>react</h2><p><code>react</code>看的各种道听途说的文章，这玩意大概是怎么玩的呢？简单的说<code>jsx</code>被解析成<code>createElement</code>的方法，里面传到参数啊什么的就是告诉<code>react</code>怎么渲染之类，然后方法返回一个<code>虚拟dom</code>,大概是这么搞的，具体细节我也不是太懂。</p>
<h2 id="vue"><a href="#vue" class="headerlink" title="vue"></a>vue</h2><p><code>vue</code>一开始让我挺晕的，官网的学习教程和一般写的项目不太一样，官网教程的例子没有单文件，全是在选项对象中加入一个<code>template</code>属性，一般的项目开发的都是用的<code>.vue</code>的单文件。一开始我是没太理解<code>.vue</code>这文件到底是怎么玩，只是按着这样写，不理解原理好像也能够基本完成自己想要的效果。</p>
<p>最近看了一些文章以及看了好几遍官网的<code>api</code>，<a href="https://cn.vuejs.org/v2/api/" target="_blank" rel="noopener">官网的api</a>还是很有必要每一个都看一遍的，之前大致看一遍感觉好多东西都没有见到过，别人代码看多了，发现很多没用的<code>api</code>还是很强的。下面讲讲我对<code>vue</code>的一些理解。</p>
<p><code>vue</code>是怎么玩的呢？每一个<code>vue</code>组件都是一个<code>Vue</code>实例，这样说也不是很准确，应该每一个自己写的组件都是通过调用<code>Vue.extend</code>继承<code>Vue</code>后扩展出来的新的类的实例，想一想怎么定义一个<code>vue</code>组件，就是调用了<code>Vue.component</code><a href="https://cn.vuejs.org/v2/api/?#Vue-component" target="_blank" rel="noopener">方法</a>，</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 注册组件，传入一个扩展过的构造器</span></span><br><span class="line">Vue.component(<span class="string">'my-component'</span>, Vue.extend(&#123; <span class="comment">/* ... */</span> &#125;))</span><br><span class="line"></span><br><span class="line"><span class="comment">// 注册组件，传入一个选项对象 (自动调用 Vue.extend)</span></span><br><span class="line">Vue.component(<span class="string">'my-component'</span>, &#123; <span class="comment">/* ... */</span> &#125;)</span><br><span class="line"></span><br><span class="line"><span class="comment">// 获取注册的组件 (始终返回构造器)</span></span><br><span class="line"><span class="keyword">var</span> MyComponent = Vue.component(<span class="string">'my-component'</span>)</span><br></pre></td></tr></table></figure>
<p>一般情况下我们写的<code>.vue单文件</code>暴露出来的就是一个选项对象，这个方法的内部自动调用了<code>Vue.extend</code>。所以<code>.vue单文件</code>本质上是<code>vue-loader</code>去把<code>&lt;template&gt;&lt;/template&gt;</code>里的内容转成字符串形式，塞到<code>&lt;scirpt&gt;&lt;/script&gt;</code>里面的<code>export default {}</code>的对象里面的<code>template</code>去，当然了，这还没有完，<code>template</code>属性里面的字符串也可以说是一种糖，<code>vue</code>内部是要调用<code>Vue.compile</code>去做一次转换，最终把<code>template</code>的值转换成<code>render</code>，这个属性的值是一个方法，这里就和<code>react</code>统一了，用是有个同名的函数<code>createElement</code>去生成<code>Vnode</code>。所以在选项对象中，可以不给出<code>template</code>属性而是给出<code>render</code>属性，同时存在<code>template</code>和<code>render</code>的时候会忽视<code>template</code>。</p>
<p>看看<a href="https://cn.vuejs.org/v2/guide/render-function.html" target="_blank" rel="noopener">官网</a><code>render</code>用法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> getChildrenTextContent = <span class="function"><span class="keyword">function</span> (<span class="params">children</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> children.map(<span class="function"><span class="keyword">function</span> (<span class="params">node</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> node.children</span><br><span class="line">      ? getChildrenTextContent(node.children)</span><br><span class="line">      : node.text</span><br><span class="line">  &#125;).join(<span class="string">''</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Vue.component(<span class="string">'anchored-heading'</span>, &#123;</span><br><span class="line">  render: <span class="function"><span class="keyword">function</span> (<span class="params">createElement</span>) </span>&#123;</span><br><span class="line">    <span class="comment">// create kebabCase id</span></span><br><span class="line">    <span class="keyword">var</span> headingId = getChildrenTextContent(<span class="keyword">this</span>.$slots.default)</span><br><span class="line">      .toLowerCase()</span><br><span class="line">      .replace(<span class="regexp">/\W+/g</span>, <span class="string">'-'</span>)</span><br><span class="line">      .replace(<span class="regexp">/(^\-|\-$)/g</span>, <span class="string">''</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> createElement(</span><br><span class="line">      <span class="string">'h'</span> + <span class="keyword">this</span>.level,</span><br><span class="line">      [</span><br><span class="line">        createElement(<span class="string">'a'</span>, &#123;</span><br><span class="line">          attrs: &#123;</span><br><span class="line">            name: headingId,</span><br><span class="line">            href: <span class="string">'#'</span> + headingId</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;, <span class="keyword">this</span>.$slots.default)</span><br><span class="line">      ]</span><br><span class="line">    )</span><br><span class="line">  &#125;,</span><br><span class="line">  props: &#123;</span><br><span class="line">    level: &#123;</span><br><span class="line">      type: <span class="built_in">Number</span>,</span><br><span class="line">      required: <span class="literal">true</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<p>看的出来这种直接写<code>render</code>比起<code>template</code>要麻烦的多，所以为了简化这种写法就搞出来了一个<code>jsx</code>写法，这里又和<code>react</code>又统一了。</p>
<p><code>jsx</code>用法:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> AnchoredHeading <span class="keyword">from</span> <span class="string">'./AnchoredHeading.vue'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">new</span> Vue(&#123;</span><br><span class="line">  el: <span class="string">'#demo'</span>,</span><br><span class="line">  render: <span class="function"><span class="keyword">function</span> (<span class="params">h</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> (</span><br><span class="line">      &lt;AnchoredHeading level=&#123;<span class="number">1</span>&#125;&gt;</span><br><span class="line">        &lt;span&gt;Hello&lt;<span class="regexp">/span&gt; world!</span></span><br><span class="line"><span class="regexp">      &lt;/</span>AnchoredHeading&gt;</span><br><span class="line">    )</span><br><span class="line">  &#125;</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>
<h2 id="一个需求引发的思考"><a href="#一个需求引发的思考" class="headerlink" title="一个需求引发的思考"></a>一个需求引发的思考</h2><p>通常我们写<code>react</code>和<code>vue</code>都是一个写一个组件<code>customComp</code>，然后在别的组件要用的时候都是直接<code>&lt;customComp/&gt;</code>写进去的，但是有的组件就不能这么做了，比如一个提醒的<code>message</code>组件，我希望在<code>react</code>中是这样调用，像是一个工具类函数一般使用：</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> message <span class="keyword">from</span> <span class="string">'message.js'</span></span><br><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">'react'</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">XXXcomponent</span> <span class="keyword">extends</span> <span class="title">React</span>.<span class="title">Component</span></span>&#123;</span><br><span class="line">    ...</span><br><span class="line">    componentDidMount()&#123;</span><br><span class="line">        message.info(&#123;</span><br><span class="line">            type:<span class="string">'success'</span>,</span><br><span class="line">            text:<span class="string">'hello react!'</span></span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在<code>vue</code>中我希望是这样的调用的：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    mounted()&#123;</span><br><span class="line">        <span class="keyword">this</span>.$message(&#123;</span><br><span class="line">            type:<span class="string">'success'</span>,</span><br><span class="line">            text:<span class="string">"hello vue!"</span></span><br><span class="line">        &#125;)</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在看了一些三方库的这种组件的实现方式，来回折腾了一段时间，对<code>react</code>和<code>vue</code>理解的更深了一些。</p>
<h3 id="vue的实现"><a href="#vue的实现" class="headerlink" title="vue的实现"></a>vue的实现</h3><p>先拿<code>vue</code>说，先写一个<code>message.vue</code>的组件文件，然后在<code>index.js</code>文件中把这个<code>message.vue</code>文件<code>import</code>进来，你可以试着打印下这个<code>vue</code>文件长得什么样，其实前面也分析过了，其实引进来的就是一个选项对象，而且<code>&lt;template&gt;&lt;/template&gt;</code>标签的内容也被弄成了render属性了，前面也铺垫过了<code>vue.extend</code>，这里把引入的选项对象传入<code>vue.extend</code>中就返回了一个扩展过的<code>Vue</code>类了,然后我们手动去<code>new</code>一个<code>message</code>的组件实例出来，可以传入一个<code>propsData</code>就能把数据传给到组件内部的<code>prop</code>中去，打印下实例就会发现定义在<code>.vue</code>文件中的<code>data</code>、<code>prop</code>之类的都有，这个时候我们只是实例化了一个实例，其实在<code>document</code>上是找不到这个节点的，只需要最后在实例上调用<code>$mount(el)</code>，就能把实例挂载到<code>document</code>上去了。直接修改实例上<code>data</code>里面定义的值，会发现也是响应的，至此这个<code>message</code>组件我们能够手动去控制这个组件了，具体细节该怎么暴露出<code>api</code>来个外部调用就不说了。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//伪代码</span></span><br><span class="line"><span class="keyword">import</span> message <span class="keyword">from</span> <span class="string">'message.vue'</span></span><br><span class="line"><span class="keyword">import</span> Vue <span class="keyword">from</span> <span class="string">'vue'</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> Constructor = Vue.extend(message) <span class="comment">//扩展出一个新的类</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> options = &#123;</span><br><span class="line">    innerProps:<span class="string">"inner props"</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> instance = <span class="keyword">new</span> Constructor(&#123;<span class="attr">propsData</span>:options&#125;)  <span class="comment">//创建一个实例，也可以传给一个props</span></span><br><span class="line"></span><br><span class="line">instance.$mount(<span class="built_in">document</span>.body) <span class="comment">//组件挂在body下</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//如果组件内部methods有方法，实例就能直接调用</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// instance.innerFunction()</span></span><br></pre></td></tr></table></figure>
<h3 id="react实现"><a href="#react实现" class="headerlink" title="react实现"></a>react实现</h3><p>大体上是和<code>vue</code>类似，有几个地方有变化，引进来的组件，因为是用<code>class extends React.Component</code>的形式写的，看的出来已经是扩展过<code>react</code>后组件类了，直接去<code>new</code>就能拿到实例了,或者是用<code>React.createElement</code>传入组件拿到<code>虚拟dom</code>。和<code>vue</code>不同的事，<code>vue</code>有个<code>$mount</code>方法帮助挂载组件到指定的位置，在<code>react</code>中要用到<code>react-dom</code>里面的<code>render</code>方法，传入前面拿到的<code>虚拟dom</code>以及要挂载的位置，这个方法会返回这个组件实例，这个时候就可以调用组件实例的<code>setState</code>方法去做一些事情了。</p>
<h2 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h2><p><code>react</code>、<code>vue</code>随便用用带还是挺简单的，我的观点是<code>api文档</code>只能教会你<strong>70%</strong>的东西，但是你有了这<strong>70%</strong>的功力，大部分的东西基本都没什么问题，但是文档之外的<strong>30%</strong>，往往是最难学的，可能需要剥开<code>vue</code>、<code>react</code>简单易用的外表，来回看官方文档，每次都能有新的体会，翻看第三方优秀的组件库是最佳学习的方案，深入地去理解内部原理才能真正的掌握。</p>
<p>这种组件函数式用法在我的<a href="https://github.com/limengke123/fantastic-carnival" target="_blank" rel="noopener">fantastic-carnival(一个博客系统)</a>中有体现：<br>其中有一个<code>loadingBar</code>组件是一个加载条的组件，参考的是<code>iview</code>组件库中的<code>vue</code>实现，同时我用<code>react</code>也实现了一遍。</p>
<ul>
<li><a href="https://github.com/limengke123/fantastic-carnival/tree/master/admin/src/components/general/loading-bar" target="_blank" rel="noopener">vue的loadingBar实现</a></li>
<li><a href="https://github.com/limengke123/fantastic-carnival/tree/master/admin/src/components/general/message" target="_blank" rel="noopener">vue的message实现</a></li>
<li><a href="https://github.com/limengke123/fantastic-carnival/tree/master/client/src/components/common/loading-bar" target="_blank" rel="noopener">react的lonadingBar实现</a></li>
</ul>
<h2 id="参考资料"><a href="#参考资料" class="headerlink" title="参考资料"></a>参考资料</h2><ol>
<li><a href="https://nlush.com/blog/2017/10/14/VUE-%E7%BB%84%E4%BB%B6%E7%9A%84%E4%B8%80%E7%82%B9%E6%80%9D%E8%80%83/" target="_blank" rel="noopener">vue组件思考</a></li>
<li><a href="https://cn.vuejs.org/v2/guide/render-function.html" target="_blank" rel="noopener">vue官网渲染函数</a></li>
</ol>
<hr>
<p><img src="/2018/08/03/vue与react组件对比学习/877090454.jpg" alt="Ivan Konstantinovich Aivazovsky – Tower. Shipwrecked"></p>
<blockquote>
<p>Ivan Konstantinovich Aivazovsky – Tower. Shipwrecked 1847</p>
</blockquote>

      
    </div>

    

    
    
    

    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/react/" rel="tag"><i class="fa fa-tag"></i> react</a>
          
            <a href="/tags/vue/" rel="tag"><i class="fa fa-tag"></i> vue</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/08/02/vue较少关注的地方/" rel="next" title="vue较少关注的地方">
                <i class="fa fa-chevron-left"></i> vue较少关注的地方
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/08/03/vue、react隐式实例化/" rel="prev" title="vue、react隐式实例化">
                vue、react隐式实例化 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">Mark Lee</p>
              <p class="site-description motion-element" itemprop="description">折腾小久的博客,在 2018-08-01 终于基本成型啦，以后就常驻啦</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">26</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  <a href="/categories/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">4</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  <a href="/tags/index.html">
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">20</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          
          

          
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#vue与react组件对比学习"><span class="nav-number">1.</span> <span class="nav-text">vue与react组件对比学习</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#前言"><span class="nav-number">1.1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#react"><span class="nav-number">1.2.</span> <span class="nav-text">react</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vue"><span class="nav-number">1.3.</span> <span class="nav-text">vue</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#一个需求引发的思考"><span class="nav-number">1.4.</span> <span class="nav-text">一个需求引发的思考</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#vue的实现"><span class="nav-number">1.4.1.</span> <span class="nav-text">vue的实现</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#react实现"><span class="nav-number">1.4.2.</span> <span class="nav-text">react实现</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#总结"><span class="nav-number">1.5.</span> <span class="nav-text">总结</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#参考资料"><span class="nav-number">1.6.</span> <span class="nav-text">参考资料</span></a></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Mark Lee</span>

  

  
</div>


  


<!--

  <div class="powered-by">由 <a class="theme-link" target="_blank" rel="external nofollow" href="https://hexo.io">Hexo</a> 强力驱动 v3.7.1</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" rel="external nofollow" href="https://theme-next.org">NexT.Pisces</a> v6.3.0</div>

-->



        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
	
    

    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.3.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.3.0"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=6.3.0"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=6.3.0"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.3.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.3.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.3.0"></script>



  



  










  





  

  

  

  

  
  

  

  

  

  

  

</body>
</html>
